package image.filter;

public class GaussianFilter extends Filter{

	private double squaredSigma;
	private double[][] gaussianMatrix;
	public GaussianFilter(double squaredSigma) {
		this.squaredSigma = squaredSigma;
		this.dimension = (int) Math.floor((2 * Math.sqrt(squaredSigma) + 1));
		this.gaussianMatrix = new double[dimension][dimension];

		for (int i = 0; i < dimension; i++) {
			for (int j = 0; j < dimension; j++) {
				gaussianMatrix[i][j] = 	gaussianValue(i-1,j-1);
			}
		}
	
	}
	
	private double gaussianValue(int x, int y){
		double first, expValue;
		first = 1 / (Math.PI * 2 * squaredSigma);
		expValue = -(double)(x * x + y * y)/squaredSigma;
	//	System.out.println("hola:" + first * Math.exp(expValue));
		return first * Math.exp(expValue);
	}
	@Override
	protected int calculateValue(int[][] mask) {
		int ans = 0;
		for (int i = 0; i < mask.length; i++) {
			for (int j = 0; j < mask[i].length; j++) {
				ans += mask[i][j] * gaussianMatrix[i][j] ;
			}
		}
		return ans;
	}
}
